package com.decheng.controller;

import com.decheng.entities.CommonResult;
import com.decheng.entities.Payment;
import com.decheng.service.PaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author 孔德成
 * @Slogan 致敬大师，致敬未来的你
 * @date 2021/8/18
 */
@Api(tags = {"支付接口"})
@RestController
@Slf4j
public class PaymentController {


    PaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;

    public PaymentController(PaymentService paymentService) {
        this.paymentService = paymentService;
    }


    @ApiOperation("创建支付")
    @PostMapping("/payment/create")
    public CommonResult create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("***** 插入操作返回结果 :" + result);
        if (result > 0) {
            return new CommonResult(200, "插入数据库成功", result);
        } else {
            return new CommonResult(444, "插入数据库失败");
        }
    }

    @ApiOperation("创建支付流水信息")
    @GetMapping("/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable Long id) {
        Payment result = paymentService.getPaymentById(id);
        log.info("***** 查询结果 :" + result);
        // 模拟耗时请求, feign 调用默认只等待1秒，超过会抛出 read time out 异常，需要设置超时等待时间
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (result != null) {
            return new CommonResult(200, "查询成功", result);
        } else {
            return new CommonResult(444, "没有对应记录，查询ID：" + id);
        }
    }

    @GetMapping(value = "/payment/lb")
    public String paymentInfo() {
        System.out.println("*****lb from port: " + serverPort + "\t" + UUID.randomUUID());
        return serverPort + "\t" + UUID.randomUUID();
    }
}
